Mehr ggplot2!

library(tidyverse)

gapminder_dat_full <- readRDS(here::here("data", "gapminder_dat.rds"))

gapminder_dat_trend <- gapminder_dat_full %>% 
  filter(time > 1990, time < 2022, country %in% c("nga", "zaf", "deu", "rus", "chn", "ind", "bra", "usa", "egy", "aus", "mex", "jpn")) 

Facetting

ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_point() + 
  geom_line() +
  theme_bg()

Faceting

Anordnen von einer einzelnen Variable in einem Raster:

facet_wrap()

ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_point() + 
  geom_line() +
  facet_wrap(vars(country), nrow = 4) +
  theme_bg()

facet_grid

ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_point() + 
  geom_line() +
  facet_grid(country ~ .) +
  theme_bg()

Facetting - Mehrere Variablen

Anordnen von einer mehreren Variable in einem Raster: ::: {.columns}

facet_wrap()

ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_point() + 
  geom_line() +
  facet_wrap(vars(country, world_4region), nrow = 4) +
  theme_bg()

facet_grid

ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_point() + 
  geom_line() +
  facet_grid(country ~ world_4region) +
  theme_bg()

:::

Facetting - Tipps

https://ggplot2-book.org/facet.html#sec-facet-wrap

Plott alle Punkte

gapminder_dat_trend_all <- gapminder_dat_trend %>% 
  select(-country)


ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_point(data = gapminder_dat_trend_all, colour = "grey") +
  geom_point() + 
  geom_line() +
  facet_wrap(vars(country)) +
  theme_bg()

bg <- gapminder_dat_trend %>%
  mutate(country_bg = country) %>%
  select(-country)

ggplot(gapminder_dat_trend, aes(x = time, y = co2_pcap_cons, color = country, group = country)) +
  # background lines: drawn in every facet, grouped by country_bg
  geom_line(
    data = bg,
    aes(x = time, y = co2_pcap_cons, group = country_bg),
    inherit.aes = FALSE,
    color = "grey70",
    alpha = 0.5,
    linewidth = 0.4
  ) +
  # foreground points/lines for the focal country in each facet
  geom_line(linewidth = 0.6) +
  geom_point(size = 1) +
  facet_wrap(vars(country)) +
  guides(color = "none") +
  theme_bg()

Facetting - Tipps

Plot Mittelwerte

ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_point() + 
  geom_line() +
  geom_smooth(aes(group = income_groups), color = "grey") +
  facet_wrap(vars(income_groups)) +
  theme_bg()

Scales

Logarithmisch vs linear

Koordinatensytem

Abspeichern

Vektor vs Raster (Rolfs 7)